# coding=utf-8
import time

import yaml
from uiautomator2 import xpath

from page_object.guang_page import GuangPage
from resource.path_info import *

"""
选货市场page
"""
class AddGoodsPage(GuangPage):
    """
    从自营店铺上架到橱窗
    """
    def add_goods_from_shop(self, d):
        print("add goods from shop")
        time.sleep(1)
        for i in range(1,4,1):
            for j in range(1,4,1):
                if d.xpath(goodsCheckBox_xpath % j).exists and d.xpath(goodTitle_xpath % j).exists:
                    if d.xpath(goodsCheckBox_xpath % j).info['enabled'] == "true":
                        d.xpath(goodsCheckBox_xpath % j).click()
                        goodTitle = d.xpath(goodTitle_xpath % j).get_text()[8:]
                        d.xpath(addGoods_but_xpath).click()
                        time.sleep(2)
                        return goodTitle
                        break
            time.sleep(1)
            d.swipe(0.5, 0.7, 0.5, 0.3, 0.13)

    """
    从选货市场上架到橱窗
    """
    def add_goods_from_market(self, d):
        print("add goods from market")
        if d.xpath(toMarket_but_xpath).exists:
            d.xpath(toMarket_but_xpath).click()
            time.sleep(3)
        for i in range(1,4,1):
            for j in range(1,4,1):
                if d.xpath(goodsCheckBox_xpath % j).exists and d.xpath(goodTitle_xpath % j).exists:
                    if d.xpath(goodsCheckBox_xpath % j).info['enabled'] == "true":
                        d.xpath(goodsCheckBox_xpath % j).click()
                        goodTitle = d.xpath(goodTitle_xpath % j).get_text()[8:]
                        d.xpath(addGoods_but_xpath).click()
                        time.sleep(2)
                        return goodTitle
                        break
            d.swipe(0.5, 0.7, 0.5, 0.3, 0.13)

    """
    从选货市场搜索上架到橱窗
    """
    def add_goods_search_market(self, d, goodsName):
        print("add goods search market")
        if d.xpath(toMarket_but_xpath).exists:
            d.xpath(toMarket_but_xpath).click()
            time.sleep(3)
        self.searchGoods(d,goodsName)
        for i in range(1,4,1):
            for j in range(1,4,1):
                if d.xpath(goodsCheckBox_xpath % j).exists and d.xpath(goodTitle_xpath % j).exists:
                    if d.xpath(goodsCheckBox_xpath % j).info['enabled'] == "true":
                        d.xpath(goodsCheckBox_xpath % j).click()
                        goodTitle = d.xpath(goodTitle_xpath % j).get_text()[8:]
                        d.xpath(addGoods_but_xpath).click()
                        time.sleep(2)
                        return goodTitle
                        break
            d.swipe(0.5, 0.7, 0.5, 0.3, 0.13)

    def searchGoods(self, d, goodsName):
        print("search self or market goods")
        d.xpath(search_but_xpath).click()
        d.xpath(search_input_box_xpath).click()
        d(focused=True).send_keys(goodsName)
        d.send_action('search')
        time.sleep(2)
        d.xpath(search_confirm_but_xpath).click()
        time.sleep(2)
        d.press("back")
        if d.xpath(search_input_box_xpath).exists:
            d.press("back")


        """
        从选货市场搜索上架到橱窗
        """
    def add_goods_search_self(self, d, goodsName):
        print("add goods search self")
        time.sleep(3)
        self.searchGoods(d, goodsName)
        for i in range(1, 4, 1):
            for j in range(1, 4, 1):
                if d.xpath(goodsCheckBox_xpath % j).exists and d.xpath(goodTitle_xpath % j).exists:
                    if d.xpath(goodsCheckBox_xpath % j).info['enabled'] == "true":
                        d.xpath(goodsCheckBox_xpath % j).click()
                        goodTitle = d.xpath(goodTitle_xpath % j).get_text()[8:]
                        d.xpath(addGoods_but_xpath).click()
                        time.sleep(2)
                        return goodTitle
                        break
            d.swipe(0.5, 0.7, 0.5, 0.3, 0.13)


    def switch_tab(self, d):
        print("switch_tab")
        if d.xpath(market_source1_xpath).exists:
            d.xpath(market_source1_xpath).click()
            time.sleep(1)
            d.xpath(confirm_but_xpath).click()
        time.sleep(1)
        for i in range(1,10,1):
            if i < 5:
                d.xpath(market_tab_xpath % i).click()
                self.check_goods_detail(d)
            elif i >= 5 and i <9:
                d.xpath(market_tab_xpath % 4).click()
                self.check_goods_detail(d)
            else:
                d.xpath(market_tab_xpath % 5).click()
                self.check_goods_detail(d)
        return self

    def check_goods_detail(self, d):
        print("to_goods_detail")
        d.xpath(goodTitle_xpath % 1).wait(timeout=3.0)
        goodTitle = d.xpath(goodTitle_xpath % 1).get_text()[8:].replace(' ','')
        print("get list name:%s"%goodTitle)
        d.xpath(goodHeadImg_xpath % 1).click_exists(timeout=3.0)
        d.xpath(goodsDetailTitle_xpath).wait(timeout=3.0)
        goodDetailTitle = d.xpath(goodsDetailTitle_xpath).get_text().replace(' ','')
        print("get detail name:%s" % goodDetailTitle)
        assert goodTitle == goodDetailTitle
        d.press("back")

    """
    切换目的地为【添加到仓库】
    """
    def switch_add_to_stock(self, d):
        d.xpath(switch_but_text).click()
        d.xpath(switch_to_stock_text).click()
        return self




